package com.allwees.bs.c.module.merchant.mapper;

import com.allwees.bs.c.module.base.mapper.IMapper;
import com.allwees.bs.c.module.merchant.dto.StoreDTO;
import com.allwees.bs.c.module.merchant.entity.StoreEntity;
import com.allwees.bs.c.module.product.dto.ProductDetailsDTO;
import com.allwees.bs.c.module.product.entity.ProductEntity;
import com.allwees.bs.core.modelbase.page.PageParams;
import com.allwees.bs.core.modelbase.page.PaginateDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StoreMapper implements IMapper<StoreDTO, StoreEntity> {

    @Override
    public StoreDTO entity2DTO(StoreEntity entity) {
        if (entity == null) {
            return null;
        }

        return new StoreDTO(entity);
    }

    public PaginateDTO<ProductDetailsDTO> entity2DTO(List<ProductEntity> entities, int total, PageParams pageParams) {
        if (entities == null) {
            return PaginateDTO.of(0, 0, pageParams);
        }

        PaginateDTO<ProductDetailsDTO> productsPaginateDTO = PaginateDTO.of(total, entities.size(), pageParams);
        List<ProductDetailsDTO> products = new ArrayList<>();
        entities.forEach(entity -> products.add(new ProductDetailsDTO(entity)));
        productsPaginateDTO.setResults(products);

        return productsPaginateDTO;
    }
}
